package com.sys.sysserver.controller.usercrud;

import com.sys.sysserver.domain.bean.PageBean;
import com.sys.sysserver.domain.dto.UserQueryDTO;
import com.sys.sysserver.domain.po.User;
import com.sys.sysserver.service.usercrud.UserCrudService;
import com.sys.sysserver.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserCrudController {
    private final UserCrudService userCrudService;

    /**
     * 综合查询
     *
     * @return
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, UserQueryDTO userQueryDTO) {
        log.info("\n\n查询条件:{},{},{}\n\n", pageNum, pageSize, userQueryDTO);
        PageBean pageBean = userCrudService.page(pageNum, pageSize, userQueryDTO);
        return Result.success(pageBean);
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userCrudService.getById(id);
        return Result.success(user);
    }

    @PatchMapping("/edit")
    public Result edit(@RequestBody User user) {
        userCrudService.edit(user);
        return Result.success();
    }

    @DeleteMapping("/del/{ids}")
    public Result del(@PathVariable List<Integer> ids) {
        userCrudService.del(ids);
        return Result.success();
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        log.info("\n\n新增：{}\n\n", user);
        userCrudService.add(user);
        return Result.success();
    }
}
